package pl.koziolekweb.szkola.DZDane;

// DZ-Dane/Dane.java
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Dane {

    public static void main(String[] args) {
        new Dane();
    }

    Dane() {
        String   nap = "Tablica";
        double[] tab = new double[] {1.5, 2.5, 4.5};

        info("Przed zapisem:",nap,tab);

          // zapis
        try {
            DataOutputStream dos = new DataOutputStream(
                    new FileOutputStream("Dane.dat"));

            dos.writeUTF(nap);
            dos.writeInt(tab.length);
            for (int i = 0; i < tab.length; i++)
                dos.writeDouble(tab[i]);
            dos.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

          // odczyt
        try {
            DataInputStream dis = new DataInputStream(
                new FileInputStream("Dane.dat"));

            nap = dis.readUTF();
            tab = new double[dis.readInt()];
            for (int i = 0; i < tab.length; i++)
                tab[i] = dis.readDouble();

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        info("Po odczycie:",nap,tab);
    }

    private void info(String mes,String nap, double[] tab) {
        System.out.print("\n" + mes + "\nnap = " +
                                nap + "\ntab = ");
        for (int i = 0; i < tab.length; i++)
            System.out.print(" " + tab[i]);
        System.out.println();
    }
}
